﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 转入 : Form
    {
        public 转入()
        {
            InitializeComponent();
        }

        private void button_tc_Click(object sender, EventArgs e)
        {
            this.Hide();
            菜单页面 f = new 菜单页面();
            f.Show();
        }

        private void button_qp_Click(object sender, EventArgs e)
        {
            tb_bqzt.Text = "";
            cb_ch.Text = "";
            tb_fb.Text = "";
            cb_zcks.Text = "";
            tb_nl.Text = "";
            dtp_ryrq.Text = "";
            dtp_ssrq.Text = "";
            cb_szks.Text = "";
            cb_xb.Text = "";
            tb_xm.Text = "";
            tb_yjje.Text = "";
            cb_ys.Text = "";
            tb_zd.Text = "";
            cb_zrhs.Text = "";
            tb_ZYNo.Text = "";
            cb_hldj.Text = "";
            dtp_zcrq.Text = "";
            cb_zcbq.Text = "";
            tb_fyhj.Text = "";

        }

        private void button_bc_Click(object sender, EventArgs e)
        {
            DateTime dt = System.DateTime.Now;
            int year = dt.Year;
            string HosNo = tb_ZYNo.Text.Trim();
            string PatName = tb_xm.Text.Trim();
            string PatSex = cb_xb.Text.Trim();
            string PatAge = tb_nl.Text.Trim();
            int Age = Convert.ToInt32(PatAge);
            int ds = year - Age;
            string BronYear = ds.ToString();
            string BedNo = cb_ch.Text.Trim();
            DateTime HosIndate = dtp_ryrq.Value;
            string HosDepertment = cb_szks.Text.Trim();
            string RODepartment = cb_zcks.Text.Trim();
            DateTime RODate=dtp_zcrq.Value ;
            string  ROArea =cb_zcbq.Text;
            string HosTotal = tb_fyhj.Text;
            string HosPrepay = tb_yjje.Text.Trim();
            string PatType = tb_fb.Text.Trim();
            DateTime HosSurgerydate = dtp_ssrq.Value;
            string HosRank = cb_hldj.Text.Trim();
            string HosCondition = tb_bqzt.Text.Trim();
            string HosIllness = tb_zd.Text.Trim();
            string DName = cb_ys.Text.Trim();
            string NName = cb_zrhs.Text.Trim();
            string HosArea = cb_zcbq.Text.Trim();
            int t =Convert .ToInt32 ( HosTotal);
            int p = Convert.ToInt32(HosPrepay);
            string HosBalance = Convert.ToString(p-t);
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {


                string DNo = "select DocNo from Doctor where DocName ='" + DName + "' ";
                string NNo = "select NurNo from Nurse where NurName ='" + NName + "' ";
                string BNo = "select BedNo from Bed where BedNo ='" + BedNo + "' ";
                SqlCommand D = new SqlCommand(DNo, conn);
                SqlCommand N = new SqlCommand(NNo, conn);
                SqlCommand B = new SqlCommand(BNo, conn);

                conn.Open();
                string DD = Convert.ToString(D.ExecuteScalar());
                string NN = Convert.ToString(N.ExecuteScalar());
                string BB = Convert.ToString(B.ExecuteScalar());

                string sql = string.Format("insert into Patient values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", HosNo, PatName, PatSex, BronYear, null, null, "转入");
                string str = string.Format("insert into InHospital values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}')", HosNo, BedNo, HosIndate, HosDepertment, HosPrepay, PatType, HosSurgerydate, HosRank, HosCondition, HosIllness, HosTotal, HosBalance, DD, NN, HosIndate, HosArea);
                string rol = string.Format("insert into RollOut values ('{0}','{1}','{2}','{3}')", HosNo, ROArea, RODepartment, RODate);
                string abc = string.Format("update Bed set BedCondition='已用'where Bed.BedNo='" + BB + "'");
                SqlCommand bed = new SqlCommand(abc, conn);
                SqlCommand cam= new SqlCommand(rol, conn);
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlCommand com = new SqlCommand(str, conn);

                int y = Convert.ToInt32(cam.ExecuteNonQuery());
                int n = Convert.ToInt32(cmd.ExecuteNonQuery());
                int m = Convert.ToInt32(com.ExecuteNonQuery());
                int z = Convert.ToInt32(bed.ExecuteNonQuery());
                if (n == 1 && m == 1 && z == 1&&y==1)
                {
                    MessageBox.Show("保存成功！");
                }
                else
                {
                    MessageBox.Show("保存失败！");
                }
                conn.Close();

            }
        
        }

        private void 转入_Load(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string sql = "select HosDeName from HospitalDepartment ";
                string str = "select NurName from Nurse";

                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlCommand com = new SqlCommand(str, conn);

                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
                SqlDataAdapter da2 = new SqlDataAdapter(str, conn);

                da.Fill(ds, "HospitalDepartment");

                da2.Fill(ds, " Nurse");

                cb_zcks.DisplayMember = "HosDeName";
                cb_zcks.DataSource = ds.Tables[0];
                cb_zrhs.DisplayMember = "NurName";
                cb_zrhs.DataSource = ds.Tables[1];
                Fill();
                   
            }
        }

        private void cb_szks_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                SqlCommand sql = new SqlCommand();
                sql.Connection = conn;
                sql.CommandText = "select DocName from Doctor where DocSubject='" + this.cb_szks.Text + "'";
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = sql;
                DataTable Doctor = new DataTable();
                sda.Fill(Doctor);
                this.cb_ys.DataSource = Doctor;
                this.cb_ys.DisplayMember = "DocName";

                string ssql = "select BedNo from Bed where BedSubject='" + this.cb_szks.Text + "'and BedCondition='未用'";
                SqlCommand cm = new SqlCommand(ssql, conn);
                DataSet ds = new DataSet();
                SqlDataAdapter da3 = new SqlDataAdapter(ssql, conn);
                da3.Fill(ds, "Bed");
                cb_ch.DisplayMember = "BedNo";
                cb_ch.DataSource = ds.Tables[0];


                conn.Close();
            }
        }


        void Fill()
        {


            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string sql = "select HosDeName from HospitalDepartment ";
               
                SqlCommand cmd = new SqlCommand(sql, conn);
               

                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(sql, conn);
               

                da.Fill(ds, "HospitalDepartment");


                cb_szks.DisplayMember = "HosDeName";
                cb_szks.DataSource = ds.Tables[0];
             

            }
        }



     
       
    }
}
